package cn.wolfcode.shop.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;

import cn.wolfcode.shop.domain.Catalog;
import cn.wolfcode.shop.domain.Product;
import cn.wolfcode.shop.domain.ProductSku;
import cn.wolfcode.shop.domain.SkuProperty;
import cn.wolfcode.shop.domain.SkuPropertyValue;
import cn.wolfcode.shop.mapper.CatalogMapper;
import cn.wolfcode.shop.mapper.ProductSkuMapper;
import cn.wolfcode.shop.mapper.ProductSkuPropertyMapper;
import cn.wolfcode.shop.service.IProductSkuService;
import cn.wolfcode.shop.vo.GenerateSkuVo;
import cn.wolfcode.shop.vo.ProductVo;

@Service
public class ProductSkuServiceImpl implements IProductSkuService {

	@Autowired
	private CatalogMapper catalogMapper;
	@Autowired
	private ProductSkuMapper productSkuMapper;
	@Autowired
	private ProductSkuPropertyMapper productSkuPropertyMapper;

	@Override
	public List<Map<String, Object>> generateSku(GenerateSkuVo vo) {
		Product product = vo.getProduct();
		List<SkuProperty> skuPropertyList = vo.getSkuPropertyList();
		List<SkuPropertyValue> skuPropertyValueList = vo.getSkuPropertyValueList();
		// 1.生成SKU前缀
		String codePreFix = generateCodePreFix(product);
		// 2.用map对数据进行分类,key为属性的id,value为当前id对应的书信之list
		Map<Object, List<String>> mapList = getMapList(skuPropertyList, skuPropertyValueList);
		// 3.把map数据转成List<List<String>>,准备递归
		List<List<String>> receiverList = new ArrayList<>();
		skuPropertyList.forEach(skuProperty -> {
			receiverList.add(mapList.get(skuProperty.getId()));
		});
		// 4.进行递归算法,最后递归出来的结果为List<List<String>>
		List<List<String>> resultList = new ArrayList<>();
		recusive(receiverList, 0, new ArrayList<String>(), resultList);
		// 5.最后把list数据转成map到前台显示List<Map<String,Object.>,准备好的递归数据,从0index开始,,创建新的list接收递归除了sku,并将sku保存到resultList
		ArrayList<Map<String, Object>> skuList = new ArrayList<>();
		for (int i = 0; i < resultList.size(); i++) {
			// 获取遍历到的dataList
			List<String> dataList = resultList.get(i);
			// 创建的map就代表的一个完整的sku
			Map<String, Object> sku = new HashMap<>();
			sku.put("code", codePreFix + i + 1);
			sku.put("price", product.getBasePrice());
			for (int j = 0; j < skuPropertyList.size(); j++) {
				SkuProperty skuProperty = skuPropertyList.get(j);
				String skuValue = dataList.get(j);
				sku.put(skuProperty.getId() + "", skuValue);
			}
			skuList.add(sku);
		}
		return skuList;
	}

	/**
	 * 递归方法
	 * @param receiverList 递归的基础数据
	 * @param layer        递归的层级
	 * @param dataList     存放sku组合的list 
	 * @param resultList   所有sku的组合的list
	 */
	private void recusive(List<List<String>> receiverList, int layer, List<String> dataList,
			List<List<String>> resultList) {
		// 1.如果不是最后一层,需要做递归的操作
		if (layer < receiverList.size() - 1) {
			receiverList.get(layer).forEach(str -> {
				// 创建当前层级的dataList
				List<String> tempList = new ArrayList<>();
				tempList.addAll(dataList);
				tempList.add(str);
				recusive(receiverList, layer + 1, tempList, resultList);
			});
		} else {
			// 2.否则,不能递归操作,而是把拼接好的一个规格组合添加到resultList中
			receiverList.get(layer).forEach(str -> {
				// 创建当前层级的dataList
				List<String> tempList = new ArrayList<>();
				tempList.addAll(dataList);
				tempList.add(str);
				resultList.add(tempList);
			});
		}
	}

	private Map<Object, List<String>> getMapList(List<SkuProperty> skuPropertyList,
			List<SkuPropertyValue> skuPropertyValueList) {
		Map<Object, List<String>> mapList = new HashMap<>();
		skuPropertyList.forEach(skuProperty -> {
			mapList.put(skuProperty.getId(), new ArrayList<>());
		});
		skuPropertyValueList.forEach(skuPropertyValue -> {
			List<String> list = mapList.get(skuPropertyValue.getSkuPropertyId());
			list.add(skuPropertyValue.getValue());
		});

		return mapList;
	}

	private String generateCodePreFix(Product product) {
		List<Catalog> catalogList = catalogMapper.getAllParentCatalog(product.getCatalog().getId());
		catalogList.remove(0);
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < catalogList.size(); i++) {
			Catalog catalog = catalogList.get(i);
			// 如果是一级分类,则获取分类编码
			if (i == 0) {
				String catalogCode = catalog.getCode();
				sb.append(catalogCode.length() > 2 ? catalogCode.substring(0, 2) : catalogCode);
			} else {
				// 否则获取的是分类顺序(未考虑100以上的子分类)
				Integer catalogSort = catalog.getSort();
				sb.append(catalogSort < 10 ? "0" + catalogSort : catalogSort);
			}
		}
		sb.append(product.getId());
		return sb.toString();
	}  

	@Override
	public void save(ProductVo vo) {
		vo.getProductSkuList().forEach(productSku -> {
			productSku.setProductId(vo.getProduct().getId());
			productSkuMapper.insert(productSku);
			productSku.getProductSkuPropertyList().forEach(productSkuProperty -> {
				productSkuProperty.setProductSkuId(productSku.getId());
				productSkuPropertyMapper.insert(productSkuProperty);
			});
		});

	}

	@Override
	public List<ProductSku> getSkuProductById(Long productId) {
		return productSkuMapper.getSkuProductById(productId);
	}

	@Override
	public ProductSku getById(Long skuId) {
		return productSkuMapper.selectByPrimaryKey(skuId);
	}
	

}
